from django.conf import settings
from django.contrib.auth.models import User
from socialbar import constants
import logging

class SettingsBackend(object):
    """
    Authenticate against the settings ADMIN_LOGIN and ADMIN_PASSWORD.

    Use the login name, and a hash of the password. For example:

    ADMIN_LOGIN = 'admin'
    ADMIN_PASSWORD = 'sha1$4e987$afbcfcwepjd23d023-j29dh239hd321e9fc33051de'
    """

    def authenticate(self, googleUser=None, password=None):
      logging.debug('authenticate Google : check pwd')
      if constants.customBackendPwd == password:
        logging.debug('authenticate Google : Try')
        try:
            user = User.objects.get(username=googleUser.nickname())
        except User.DoesNotExist:
            # Create a new user. Note that we can set password
            # to anything, because it won't be checked; the password
            # from settings.py will.
            user = User.objects.create_user(googleUser.nickname(), googleUser.email(), password)
        return user
      return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None